package com.qckj.amz.domain.spapi.feeds.home.ProductImagesFeedXml;

import com.qckj.amz.domain.spapi.feeds.home.AmazonEnvelope;
import com.qckj.amz.domain.spapi.feeds.home.Header;
import com.qckj.amz.domain.spapi.feeds.home.Message;
import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luchuan
 * @version v1.0
 * @date 2022/11/20 0020
 **/
public class TestCreateXml {
    public static void main(String[] args) throws JAXBException {

        ProductImage productImage = new ProductImage();
        productImage.setSKU("SKU123456");
        productImage.setImageType("Main");
        productImage.setImageLocation("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF");

        /**
         * 二级/Message,此节点为List对象，可以包含主体、变体
         */
        List<Message> messageList = new ArrayList<>();
        Message message = new Message();
        message.setMessageID(1L);
        message.setOperationType("Update");
        message.setProductImage(productImage);
        messageList.add(message);

        /**
         * 二级/Header
         */
        Header header = new Header();
        header.setDocumentVersion("1.01");
        header.setMerchantIdentifier("ARBGXXE1RPNJC");

        /**
         * 一级/根节点AmazonEnvelope
         */
        AmazonEnvelope ae = new AmazonEnvelope();
        ae.setHeader(header);
        ae.setMessageType("ProductImage");
        ae.setMessage(messageList);


        /**
         * 实现java对象转xml字符串
         */
        JAXBContext jaxbContext = JAXBContext.newInstance(AmazonEnvelope.class);
        StringWriter writer = new StringWriter();
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(ae, writer);
        String xmlStr = writer.toString();
        xmlStr = StringUtils.replace(xmlStr, "&quot;", "'");

        System.out.println(xmlStr);


    }
}
